package com.tangyee.utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.math.BigDecimal;

/**
 * @author leeguiyang@yeah.net
 * @Description 这是一个对本地缓存进行清理的工具类
 *
 * Context.getExternalFilesDir() --> SDCard/Android/data/TangYeeApp/files/ 目录，一般放一些长时间保存的数据
 * Context.getExternalCacheDir() --> SDCard/Android/data/TangYeeApp/cache/目录，一般存放临时缓存数据
 */
public class LocalCacheManager {
    private static final String TAG = "LocalCacheManager";

    /**
     * 清楚缓存
     * @param context
     */
    public static void clearAllCache(Context context){
        deleteCacheFile(context.getCacheDir());//首先删除手机自带的cache
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            //如果存在sd卡并且在sd卡由缓存数据，则删除
            deleteCacheFile(context.getExternalCacheDir());
        }
    }

    public static boolean deleteCacheFile(File dir){
        //dir不为null,且是一个目录
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                //递归
                boolean success = deleteCacheFile(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        return dir.delete();
    }
    /**
     * 获取全部的缓存文件大小
     * 用于显示全部的内存大小
     * @param context
     * @return
     */
    public static String getTotalCacheSize(Context context) {
        //统计默认缓存大小
        long cacheSize = getFolderSize(context.getCacheDir());
        Log.e(TAG,"CacheSize = " +cacheSize);
        //如果挂载了SD卡，就判断SD卡下的缓存文件
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            cacheSize += getFolderSize(context.getExternalCacheDir());//获取SD卡的缓存大小
            Log.e(TAG,"after SD , CacheSize = " + cacheSize);
        }
        return getFormatSize(cacheSize);//返回格式化之后的缓存大小
    }

    /**
     * 对缓存文件大小进行单位格式化
     * @param cacheSize
     * @return
     */
    public static String getFormatSize(double cacheSize){
        Log.e(TAG,"into Cachesize = " + cacheSize );
        double kByte = cacheSize/1024;//换算成KB
        if(kByte<1){
            return cacheSize/1024+"kB";
        }
        double mByte = kByte/1024;//MB
        if(mByte<1){
            BigDecimal mDecimal = new BigDecimal(Double.toString(kByte));
            return mDecimal.setScale(1,BigDecimal.ROUND_HALF_UP).toPlainString()+"KB";
        }
        double gByte = mByte/1024;
        if(gByte<1){
            BigDecimal gDecimal = new BigDecimal(Double.toString(mByte));
            return gDecimal.setScale(1,BigDecimal.ROUND_HALF_UP).toPlainString()+"MB";
        }
        double tByte = gByte/1024;
        if (tByte<1){
            BigDecimal tDecimal = new BigDecimal(Double.toString(gByte));
            return tDecimal.setScale(1,BigDecimal.ROUND_HALF_UP).toPlainString()+"GB";
        }
        BigDecimal finalDecimal = new BigDecimal(Double.toString(tByte));
        return finalDecimal.setScale(1,BigDecimal.ROUND_HALF_UP).toPlainString()+"TB";
    }
    /**
     * 统计出一个目录下所有大文件大小
     * 获取文件大小
     * @param file
     * @return
     */
    public static long getFolderSize(File file) {
        long size = 0;
        //返回某个目录下所有文件和目录的绝对路径，返回的是File数组
        try {
            File[] fileList = file.listFiles();
            for (int i = 0; i < fileList.length; i++) {
                if (fileList[i].isDirectory()) {
                    //是目录就递归
                    size += getFolderSize(fileList[i]);
                } else {
                    size += fileList[i].length();
                }
            }
        } catch (Exception e) {
            //可能会出现溢出等
            e.printStackTrace();
        }
        return size;
    }
}
